<template>
  <div class="table-default">
    <p style="padding-left: 1rem; font-weight: bold; margin-bottom: 1rem;">
      <el-icon style="margin-right: 0.5rem;font-weight: bold;"><Warning /></el-icon>请先确认金蝶仓库库存是否存在 <span style="color: red">未审核</span> 单据
    </p>
    <el-form :model="formData" label-width="120px" ref="form" v-loading="loading" class="pr-4" readonly>
      <el-form-item label="盘点方案名称 :" prop="plan_name">
        <el-input v-model="formData.plan_name" name="plan_name" clearable :readonly="formData.status == 1" />
      </el-form-item>
      <el-form-item label="分单规则 :" prop="split_order_rules">
        <el-select v-model="formData.split_order_rules" name="split_order_rules" placeholder="请选择分单规则" clearable>
          <el-option :label="item.label" :value="item.value" v-for="item in SplitOrderRules" :key="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="库存组织 :" prop="org_code">
        <el-select v-model="formData.org_code" name="org_code" placeholder="请选择库存组织" clearable>
          <el-option :label="item.label" :value="item.value" v-for="item in OrgList" :key="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库 :" prop="wms_code">
        <el-select v-model="formData.wms_code" name="wms_code" placeholder="请选择仓库" clearable>
          <el-option :label="item.fname" :value="item.fnumber" v-for="item in storeList" :key="item.fnumber" />
        </el-select>
      </el-form-item>
      <el-form-item label="盘点范围" prop="wms_range">
        <el-input v-model="formData.wms_range" name="wms_range" placeholder="选择库位" @click="storageShow = true" />
      </el-form-item>
      <!-- 仓库代码：{{ formData.wms_code }} -->

      <el-form-item label="备注 :" prop="remark">
        <el-input v-model="formData.remark" name="remark" clearable />
      </el-form-item>
      <el-form-item prop="allow_new_material">
        <el-checkbox v-model="formData.allow_new_material" label="允许盘点新增物料" />
      </el-form-item>
      <!-- <el-form-item prop="no_check_order">
        <el-checkbox v-model="formData.no_check_order" label="忽略单据库存检查" />
      </el-form-item> -->
      <!-- <el-form-item prop="create_adjust_order">
        <el-checkbox v-model="formData.create_adjust_order" label="创建盘盈盘亏单" />
      </el-form-item> -->

      <!-- <el-form-item label="实盘数默认值 :" prop="default_qty">
        <el-input v-model="formData.default_qty" name="default_qty" type="number" clearable />
      </el-form-item> -->
      <!-- <el-form-item label="库存截止时间 :" prop="inventory_eadline">
        <el-radio-group v-model="radioValue" class="ml-4">
          <el-radio label="0" size="large">即使库存</el-radio>
          <el-radio label="1" size="large">截止日期</el-radio>
<<<<<<< HEAD

=======
>>>>>>> inventory
          <el-date-picker v-if="radioValue == '1'" v-model="formData.inventory_eadline" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" placeholder="选择截止日期" />
        </el-radio-group>
      </el-form-item> -->

      <div class="flex justify-end">
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>

      <el-dialog v-model="storageShow" title="选择仓库" width="80%" :before-close="handleClose" class="dialog" style="height: 80vh">
        <createStorage @send-data="getDataHandler" :showList="showList" :wmsCode="formData.wms_code" />
      </el-dialog>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { useCreate } from '/admin/composables/curd/useCreate'
import { useShow } from '/admin/composables/curd/useShow'
import { onMounted, ref, watch } from 'vue'
import createStorage from './createStorage.vue'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'
import { Warning } from '@element-plus/icons-vue'
const props = defineProps({
  primary: String | Number,
  api: String,
})

// const { formData, form, loading, submitForm, close } = useCreate(props.api, props.primary)
let resData = ref({})
let formData = ref({})

let radioValue = ref('0')
// primary为id 有即修改
console.log('primary', props.api, props.primary)

if (props.primary == null) {
  console.log('进去新增')

  ElMessage.warning('请先确认金蝶仓库库存是否存在未审核单据！')

  formData.value = {
    default_qty: 0,
    allow_new_material: 0,
    no_check_order: 0,
    create_adjust_order: 0,
    inventory_eadline: '0',
  }
}

const emit = defineEmits(['close'])

// watch(radioValue, (newVal, oldVal) => {
//   if (newVal == '0') {
//     console.log('库存选项改变')
//     formData.value.inventory_eadline = '0'
//     console.log(formData.value.inventory_eadline)
//   }
// })

let storageShow = ref(false)

// 提交
let submitForm = () => {
  console.log('保存', formData.value)
  if (props.primary) {
    http.put(`inventory/plan/${props.primary}`, formData.value).then(r => {
      ElMessage.success(r.data.message)
      emit('close')
    })
  } else {
    http.post(`inventory/plan`, formData.value).then(r => {
      ElMessage.success(r.data.message)
      emit('close')
    })
  }
}

// let sotrageClick = () => {
//   console.log('点击')
// }

let confirmStorage = () => {
  // formData.value.wms_range = ['a03']
  storageShow.value = false
}

let showList = ref([])
let wmsCode = ref('')

let getDataHandler = data => {
  // showList.value = data.map(item => {
  //   return { warehousecode: item }
  // })
  // showList.value = data
  // console.log(showList.value);
  storageShow.value = false
  formData.value.wms_range = data
}

const closeHandler = () => {
  close(() => emit('close'))
}

let OrgList = ref([])
let storeList = ref([])
let SplitOrderRules = ref([])

const enumName = () => {
  http.get(`inventory/plan/${props.primary}`).then(r => {
    r.data.data.org_code = Number(r.data.data.org_code)
    r.data.data.default_qty = Number(r.data.data.default_qty)
    resData.value = r.data.data
    console.log('详情数据', resData.value)
    if (props.primary) {
      formData.value = resData.value
      // useShow(props.api, props.primary, formData)

    }
  })
  http.get(`options/Org`).then(r => {
    OrgList.value = r.data.data
    console.log('组织机构', OrgList.value)
  })
  // http.get(`warehouse/datatype?`).then(r => {
  //   storeList.value = r.data.data
  //   console.log('仓库数据', storeList.value)
  // })

  http.get(`options/SplitOrderRules`).then(r => {
    SplitOrderRules.value = r.data.data
    console.log('盘点方案-拆单规则	', SplitOrderRules.value)
  })
}
enumName()

const getwarehouse =  (data) =>{
  data = data ? data : ''
  http.get(`warehouse/datatype?fuseorgid_number=${data}`).then(r => {
    storeList.value = r.data.data
    console.log('仓库数据', storeList.value)
  })
}

getwarehouse(null)

watch(formData.value, (newVal, oldVal) => {
  console.log('监听变化',newVal);
  if(newVal.org_code) {
    console.log('监听变化库存组织',newVal.org_code);
    getwarehouse(newVal.org_code)
  }
})

onMounted(() => {
  close(() => emit('close'))
})
</script>

<style lang="css">
.dialog {
  /* height: 85%; */
  overflow: auto;
}
.el-form-item {
  margin-bottom: 10px;
}
.btn {
  padding: 20px;
}
</style>
